package com.xcc.manager.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

/**
 * @outhor Ren
 * @create 2019-03-03 0:46
 * 描述:
 */
@Component
public class ManagerFilter extends ZuulFilter {

    /**
     * 过滤器类型:  pre 请求前执行, post 请求后执行
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 优先及, 数字越小优先级越高
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 是否开启过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器内的执行草案做, return 任何 object 值,都表示继续执行
     *
     * RequestContext requestContext = RequestContext.getCurrentContext();
     * setSendZuulResponse 设置为 false 表示不继续执行
     * requestContext.setSendZuulResponse(false);
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        String headerTest = requestContext.getRequest().getHeader("test");
        // setSendZuulResponse 设置为 false 表示不继续执行
//        requestContext.setSendZuulResponse(false);
        // 将请求头继续转发
        System.out.println("filter hear: " + headerTest);
        requestContext.addZuulResponseHeader("test", headerTest);
        return null;
    }
}
